import { ref, unref } from 'vue';
import dayjs from 'dayjs';
import { minuteToStr } from '/@/utils/utils.ts';

function timeFormat(type, list) {
    const o = {
        week: 'MM-DD',
        month: 'MM-DD',
        year: 'YYYY-MM',
    };
    if (list) {
        return list.map((item) => dayjs(item).format(o[type]));
    }
    return [];
}

//时间转换
export function formatStr({ list, len }) {
    let sum =
        list.reduce((all, cur) => {
            return all + cur;
        }, 0) / len;

    return minuteToStr(sum);
}

function createWeek() {
    let arr = [];
    for (let i = 0; i < 7; i++) {}
}

const defaultVal = (type) => {
    if (type == 'week') {
        return {
            defaultTime: [],
        };
    }
    if (type == 'month') {
    }
    if (type == 'year') {
    }
};

export function sleepCharts({ time, short, long, awake, type }) {
    let middleIndex = Math.floor(time.length / 2);
    return {
        grid: {
            top: '15%',
            left: '5%',
            right: '5%',
            bottom: 30,
        },
        tooltip: {
            show: true,
            trigger: 'axis',
            textStyle: {
                align: 'left', // 使用左对齐样式显示tooltip文本
                color: '#fff',
            },
            backgroundColor: 'rgba(50,50,50,0.7)',
            borderColor: '#1b7ef2',
        },
        legend: {
            orient: 'horizontal',
            data: ['清醒', '浅睡', '深睡'],
            textStyle: {
                color: '#ffffff',
            },
            icon: 'roundRect',
            height: 40,
            itemHeight: 14,
            top: -5,
            right: '15%',
            selectedMode: false,
        },
        color: ['#14B3F8', '#6D7BEB', '#5451DC'],
        xAxis: {
            type: 'category',
            axisTick: {
                show: false,
            },
            axisLabel: {
                color: '#ffffff',
                formatter: (item, i) => {
                    let mid = time.length == 30 ? 16 : middleIndex;
                    if (i == 0 || i == mid || i == time.length - 1) {
                        return item;
                    }
                },
                showMinLabel: true,
                showMaxLabel: true,
            },
            data: timeFormat(type, time),
        },
        yAxis: {
            type: 'value',
            axisLabel: {
                show: false,
            },
            axisLine: {
                show: false,
            },
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                    color: '#696969',
                },
            },
        },
        series: [
            {
                name: '清醒',
                type: 'bar',
                data: awake,
            },
            {
                name: '浅睡',
                type: 'bar',
                data: short,
            },
            {
                name: '深睡',
                type: 'bar',
                data: long,
            },
        ],
    };
}

//步数
export function useStep() {
    const total = ref();
    const stepData = ref([
        {
            name: '5000以下',
            value: '0',
            type: 'lessThanFiveThousand',
            type_dictText: '恶性肿瘤',
        },
        {
            name: '5000~1w',
            value: '0',
            type: 'fiveThousandToTenThousand',
            type_dictText: '其他',
        },
        {
            name: '1~1.5w',
            value: '0',
            type: 'tenThousandToFifteenThousand',
            type_dictText: '其他',
        },
        {
            name: '1.5w~2w',
            value: '0',
            type: 'fifteenToTwentyThousand',
            type_dictText: '其他',
        },
        {
            name: '2w以上',
            value: '0',
            type: 'twentyThousandOrMore',
            type_dictText: '其他',
        },
    ]);

    function setStep(data) {
        stepData.value.map((item) => {
            item.value = data[item.type] || 0;
        });
        total.value = data?.userCount;
    }

    function getStepData() {
        return unref(stepData);
    }

    function getTotal() {
        return unref(total);
    }

    return {
        total,
        stepData,
        setStep,
        getStepData,
        getTotal,
    };
}

export const monitorColumns = [
    {
        title: '姓名',
        dataIndex: 'userName',
        key: 'userName',
        align: 'center',
    },
    {
        title: '类型',
        dataIndex: 'eventType_dictText',
        key: 'eventType_dictText',
        align: 'center',
    },
    {
        title: '异常时间',
        dataIndex: 'warnTime',
        key: 'warnTime',
        align: 'center',
        width: 150,
    },
    {
        title: '定位',
        key: 'address',
        align: 'center',
        slots: { customRender: 'address' },
        width: 40,
    },
];
